package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domain.Category;
import com.itheima.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author licheng
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> categorySave(@RequestHeader("Authorization")String token, @RequestBody Category category) {
        log.info("新增分类信息：{}", category.toString());
        categoryService.save(category);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page<Category>> categoryPageSelect(@RequestParam(value = "name",required = false)String name, @RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        log.info("page={},pageSize={},name={}", page, pageSize, name);
        return  categoryService.adminCategoryPageSelect(name,page,pageSize);
    }

    @GetMapping("/list")
    public R<List<Category>> categoryList( @RequestParam("type") String type ){
        log.info("显示分类的类型 type={}", type);
        return categoryService.adminCategoryList(type);
    }
    /**
     * @param category 修改的员工信息
     * @return 修改结果
     */
    @PutMapping
    public R<String> categoryUpdate (@RequestBody Category category){
        log.info("修改分类信息：{}", category.toString());
        return categoryService.adminCategoryUpdate(category);
    }

    @DeleteMapping
    public R<String> categorydelete(@RequestParam("id") String id){
        log.info("删除分类信息的 id={}",id);
        return categoryService.adminCategoryDelete(id);
    }








}
